package web;

import javax.faces.bean.ManagedBean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.faces.application.FacesMessage;  
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;  
import org.primefaces.event.FileUploadEvent;
  
import org.primefaces.model.UploadedFile;  


@ManagedBean
@ViewScoped
public class FileUploadController {  
  
    private String nombre;
     
    private UploadedFile file;  
     
    public UploadedFile getFile() {  
        return file;  
    }  
   
    public void setFile(UploadedFile file) {  
        this.file = file;  
    }  
   
    public void upload() {  
        FacesMessage msg = new FacesMessage("Ok", "Fichero " + file.getFileName() + " subido correctamente.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }  
     


    public void handleFileUpload(FileUploadEvent event) {
       
        try {
            File targetFolder = new File("/Users/Denver/NetBeansProjects/BirrApp/BirrApp-war/web/images/imageslocal");
            String filename = targetFolder.getName();
            InputStream inputStream = event.getFile().getInputstream();
            OutputStream out = new FileOutputStream(new File(targetFolder,
                    event.getFile().getFileName()));
            int read = 0;
            byte[] bytes = new byte[2048];

            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            inputStream.close();
            out.flush();
            out.close();
            
//            copiaServer("/upload/",event.getFile().getFileName(), inputStream);
            setNombre(event.getFile().getFileName());
        } catch (IOException e) {
            e.printStackTrace();
        
       
        }
    }
    public void setNombre(String nombre){
        this.nombre =nombre;
    }
    
    public String getNombre(){
        return nombre;
    }
     
//    public void copiaServer(String destino, String filename, InputStream in){
//        
//         String destination=destino + filename;
//        
//          
//        
//          FacesContext facesContext = FacesContext.getCurrentInstance();
//        ExternalContext externalContext = facesContext.getExternalContext();
//            String realPath = externalContext.getRealPath(destination);
//         
//           System.out.println("Writting images in " + realPath);
//           File f= new File(realPath);
//           
//           try {
//                // write the inputStream to a FileOutputStream
//
//                OutputStream out = new FileOutputStream(new File(realPath));
//             
//                int read = 0;
//                byte[] bytes = new byte[1024];
//             
//                while ((read = in.read(bytes)) != -1) {
//                    out.write(bytes, 0, read);
//                }
//             
//                in.close();
//                out.flush();
//                out.close();
//             
//                System.out.println("New file created!! : " + realPath + filename);
//                } catch (IOException e) {
//                System.out.println(e.getMessage());
//             }
//    
//    }
}


 